import Clipboard from 'clipboard'

export function isNull(value) {
  return (value === null) || (value === undefined);
}

export function isNotNull(value) {
  return (value !== null) && (value !== undefined);
}

export function isEmptyStr(str) {
  // return (str === undefined) || (!str) || (!/[^\s]/.test(str));
  return (str === undefined) || (!str && (str !== 0) && (str !== '0')) || (!/[^\s]/.test(str));
}

export function $createCode(length = 12) {
  let code = ''
  while (code.length < length) {
    code += Math.random().toString(36).replace('0.', "").slice(Math.min(6, length - code.length) * -1);
  }
  return code
}

export function sleep(millisecond = 500) {
  return new Promise((resolve) => {
    setTimeout(() => {
      return resolve(void 0);
    }, millisecond);
  });
}

export function $createUid(length = 8, key = 'UID', join = '-') {
  return [key, join, $createCode(length)].join('');
}

export const deepClone = function (origin) {
  if (origin === undefined) {
    return undefined
  }
  return JSON.parse(JSON.stringify(origin))
}

export function deepChange(data1, data2) {
  if (!data1 || !data2) return
  const dk1 = Object.keys({ ...data1 })
  const dk2 = Object.keys({ ...data2 })
  const changeCount = []
  dk1.filter(i => dk2.includes(i)).forEach(i => {
    if (data1[i] !== data2[i]) {
      data1[i] = data2[i]
      changeCount.push(i)
    }
  })
  dk1.filter(i => !dk2.includes(i)).forEach(i => { delete data1[i]; changeCount.push(i) })
  dk2.filter(i => !dk1.includes(i)).forEach(i => { data1[i] = data2[i]; changeCount.push(i) })
  // console.groupCollapsed(`====修改了【${changeCount.length}】个字段====`)
  // changeCount.forEach(i => console.log(i, data1[i]))
  // console.groupEnd()
}

export function getDefaultFormConfig() {
  return {
    modelName: 'formData',
    refName: 'YanForm',
    rulesName: 'rules',
    labelWidth: 80,
    labelPosition: 'left',
    size: '',
    labelAlign: 'label-left-align',
    cssCode: '',
    customClass: [],
    functions: '',
    layoutType: 'PC',

    onFormCreated: '',
    onFormMounted: '',
    onFormDataChange: '',
  }
}

export const addWindowResizeHandler = function (handler) {
  const oldHandler = window.onresize
  if (typeof window.onresize !== 'function') {
    window.onresize = handler
  } else {
    window.onresize = function () {
      oldHandler()
      handler()
    }
  }
}

export function copyToClipboard(content, clickEvent, $message, successMsg, errorMsg) {
  const clipboard = new Clipboard(clickEvent.target, {
    text: () => content
  })

  clipboard.on('success', () => {
    $message.success(successMsg)
    clipboard.destroy()
  })

  clipboard.on('error', () => {
    $message.error(errorMsg)
    clipboard.destroy()
  })

  clipboard.onClick(clickEvent)
}

// export function getDifferent(data1, data2) {
// const dk1 = Object.keys({ ...data1 })
// const dk2 = Object.keys({ ...data2 })
// console.groupCollapsed('----对象属性变更------')
// console.log('----变更----')
// dk1.filter(i => dk2.includes(i)).forEach(i => {
//   if (dk1[i] !== dk2[i]) console.log(`${i}:   ${data1[i]} => ${data2[i]}`)
// })
// console.log('----删除----')
// dk1.filter(i => !dk2.includes(i)).forEach(i => {
//   console.log(`${i}:   ${data1[i]} => `)
// })
// console.log('----新增----')
// console.log(dk2, dk1)
// dk2.filter(i => !dk1.includes(i)).forEach(i => {
//   console.log(` => ${i}:   ${data2[i]}`)
// })
// console.groupEnd()
// console.log('getDifferent', JSON.stringify(data1) !== JSON.stringify(data2))
//   return JSON.stringify(data1) !== JSON.stringify(data2)
// }
